<template>
  <div>
    <home-header></home-header>
    <home-swiper :swiperList='swiperList'></home-swiper>
    <home-icons :iconList="iconList"></home-icons>
    <home-jobtitle :city="city" :job="job"></home-jobtitle>
    <home-joblist :jobList="jobList"></home-joblist>
    



  </div>
</template>

<script>
import HomeHeader from './components/Header.vue'
import HomeSwiper from './components/Swiper.vue'
import HomeIcons from './components/Icons.vue'
import HomeJobtitle from './components/Jobtitle.vue'
import HomeJoblist from './components/Joblist.vue'
import axios from 'axios'
import { mapState } from 'vuex'



export default {
    name: 'Home',
    data(){
      return{
        lastCity: '',
        lastJob: '',
        swiperList:[],
        iconList:[],
        jobList:[]
      }
    },
    computed: {
      ...mapState({
        city: "city",
        job: "job"
      })
    },
    components:{
      HomeHeader,
      HomeSwiper,
      HomeIcons,
      HomeJobtitle,
      HomeJoblist
    },
    methods: {
      getHomeInfo(){
        axios.get('/api/homeinfo.json')
        .then(this.getHomeInfoSucc)
      },
      getJobsByCityAndJob(){
        axios.get('/api/getJobsByCityAndJob.json?city=' + this.city + '&job=' + this.job)
        .then(this.getJobsByCityAndJobSucc)
      },
      getHomeInfoSucc(res){
        if(res.status == 200 && res.data){
          const data = res.data
          this.swiperList = data.swiperList
          this.iconList = data.iconList
        }
      },
      getJobsByCityAndJobSucc(res){
        if(res.status == 200 && res.data){
          const data = res.data
          this.jobList = data.jobList
        }
      }
    },
    mounted(){
      this.lastCity = this.city
      this.lastJob = this.job
      this.getJobsByCityAndJob()
      this.getHomeInfo()

    },
    activated(){
      if(this.lastCity !== this.city){
        this.lastCity = this.city
        this.getJobsByCityAndJob()

      }
      if(this.lastJob !== this.job){
        this.lastJob = this.job
        this.getJobsByCityAndJob()

      }
    }
}
</script>

<style scoped lang="scss">

</style>